﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rbac.Domain;
using Rbac.Project.Application.Menus;
using Rbac.Project.Application.Menus.Dto;
using Rbac.Project.WebApi.Filters;
using System.Linq;
using System.Threading.Tasks;

namespace Rbac.Project.WebApi.Controllers
{
    /// <summary>
    /// 自定义授权过滤器
    /// </summary>
    [Route("/[controller]/[action]")]
    [ApiController]
    //[CustomerAuthorizationFilter]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService menuService;

        public MenuController(
            IMenuService menuService
            )
        {
            this.menuService = menuService;
        }

        /// <summary>
        /// 递归获取所有菜单项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTreeNodes()
        {
            var headers = HttpContext.Request.Headers;

            var user = HttpContext.User.Identity.Name;

            var isLogin = HttpContext.User.Identity.IsAuthenticated;

            var claims = HttpContext.User.Identities.First().Claims;


			//加日志

			return Ok(await menuService.GetTreeNodes());
        }

        /// <summary>
        /// 获取当前登录用户的菜单项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetMenu()
        {
			//加日志

			return Ok(await menuService.GetMenu());
        }

        [HttpPost]
        public async Task<IActionResult> CreateAsync(CreateUpdateDto dto)
        {
            //加日志
            return Ok(await menuService.InsertAsync(dto));
        }
    }
}
